package com.jml.jdk8.optional;


import java.util.Optional;

/**
 * @author:jml
 * @date: 2019/7/31 0031 22:20
 * @description：
 */
public class OptionalTest {

    public static void main(String[] args) {
        Optional<String> optional = Optional.of("hello");
        if(optional.isPresent()){
            System.out.println(optional.get());
        }

        optional.ifPresent(x-> System.out.println(x));

        Optional<String> optional1 = Optional.empty();
        //如果optional里面没有值就返回备用的值other
        String other = optional1.orElse("other");
        System.out.println(other);

        //如果optional里面没有值就返回备用的值other
        String s1 = optional1.orElseGet(() -> "nihao");
        System.out.println(s1);

        Optional<String> filterOptional = optional.filter(x -> x == "" ? true : false);

        boolean present = optional.filter(x -> x == "" ? true : false).isPresent();
        System.out.println(present);
    }


}
